// ---------------------------- 获取评论数据，渲染到页面中
function getComment() {
    $.ajax({
        type: "GET",
        url: "admin/comments",
        success: function(res){
            console.log(res)
            let arr = []
            if (res.status === 0) {
                res.data.forEach(item => {
                    arr.push(`
                    <tr>
                        <td>${item.id}</td>
                        <td>${item.uname}</td>
                        <td>${item.content}</td>
                        <td>${item.cdate}</td>
                        <td>
                            <button type="button" class="layui-btn layui-btn-xs layui-btn-danger" data-id="${item.id}">删除</button>
                        </td>
                    </tr>
                `)
                });
                $("tbody").html(arr.join(""))
            }
        }
    })
}
getComment()

// ---------------------------- 删除功能
let layer = layui.layer
$("tbody").on("click", 'button:contains("删除")', function(){
    let id = $(this).data("id")
    layer.confirm('确定删除该评论吗？', {icon: 3, title:'提示'}, function(index){
        $.ajax({
            type: "DELETE",
            url: "admin/comments/" + id,
            success: function(res){
                console.log(res)
                layer.msg(res.message)
                if (res.status === 0) {
                    getComment()
                }
            }
        })  
        layer.close(index);
    });
})